/**
 * 
 */
package org.statewalker.parser.wiki.tokenizer.block;

import org.statewalker.tokenizer.AbstractTokenizer;
import org.statewalker.tokenizer.CharStream;
import org.statewalker.tokenizer.StreamToken;
import org.statewalker.tokenizer.CharStream.Marker;
import org.statewalker.tokenizer.base.NewLineTokenizer;

/**
 * @author kotelnikov
 */
public class EmptyLineTokenizer extends AbstractTokenizer {

    private boolean fOpen;

    /**
     * 
     */
    public EmptyLineTokenizer(String key) {
        this(key, true);
    }

    /**
     * 
     */
    public EmptyLineTokenizer(String key, boolean open) {
        super(key);
        fOpen = open;
    }

    public StreamToken read(CharStream stream) {
        StreamToken result = null;
        Marker marker = stream.markPosition();
        try {
            int counter;
            for (counter = 0; NewLineTokenizer.readNewLine(stream) != null; counter++) {
            }
            if (counter > 1) {
                result = newToken(stream, marker);
                result.setOpen(fOpen);
                result.setClose(!fOpen);
            }
            return result;
        } finally {
            marker.close(result == null);
        }
    }

}
